package com.seu.project.seuquiz.model.vo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.model.entity.Contest;
import com.seu.project.seuquiz.model.internal.ContestTypeInternal;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author zhushoukang <zhushoukang@kuaishou.com>
 * Created on 2023-08-10
 */
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContestVO {
    /**
     * id
     */
    private Long id;

    /**
     * 比赛名称
     */
    private String contestName;

    /**
     * 开始时间
     */
    private Date startTime;

    /**
     * 结束时间
     */
    private Date endTime;

    /**
     * 单人可答题次数
     */
    private Integer answerTimes;

    /**
     * 限时（单位s）
     */
    private Integer time;

    /**
     * 各个题目类型的数量（比如：单选10道，多选5道，判断10道，简单5道）
     */
    private ContestTypeInternal[] questionNum;

//    /**
//     * 题目列表（题目id的列表，如【0，1，2，3，4】）
//     */
//    private String questionList;

    /**
     * 状态（有效/无效） 0 有效  1-无效
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    private static final long serialVersionUID = 1L;

    public static ContestVO toModel(Contest contest) {
        return ContestVO.builder()
                .id(contest.getId())
                .contestName(contest.getContestName())
                .startTime(contest.getStartTime())
                .endTime(contest.getEndTime())
                .answerTimes(contest.getAnswerTimes())
                .time(contest.getTime())
                .questionNum(constructContestType(contest.getQuestionNum(), contest.getQuestionList()))
                .status(contest.getStatus())
                .createTime(contest.getCreateTime())
                .updateTime(contest.getUpdateTime())
                .build();
    }

    private static ContestTypeInternal[] constructContestType(String questionNum, String questionList) {
        if (StringUtils.isAnyBlank(questionNum, questionList)) {
            return null;
        }

        String[] question = questionNum.split("#");
        String[] answer = questionList.split("#");

        if (question.length != answer.length) {
            return null;
        }

        ContestTypeInternal[] list = new ContestTypeInternal[question.length];

        for (int i = 0; i < question.length; i++) {
            ContestTypeInternal contestTypeInternal = new ContestTypeInternal();
            String[] res = handleQuestion(question[i]);
            if (res != null) {
                contestTypeInternal.setType(res[0]);
                contestTypeInternal.setCount(res[1]);
            }

            contestTypeInternal.setAnswerList(Arrays.stream(answer[i]
                    .split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList()));
            list[i] = contestTypeInternal;
        }
        return list;
    }

    private static String[] handleQuestion(String s) {
        String[] res = new String[2];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                res[0] = sb.toString();
                int num = Integer.parseInt(Character.toString(c));
                for (i++; i < s.length(); i++) {
                    num = num * 10 + Integer.parseInt(Character.toString(s.charAt(i)));
                }
                res[1] = Integer.toString(num);
                return res;
            } else {
                sb.append(c);
            }
        }
        return null;
    }
}
